#辅助工具
import datetime
import random

user={
    'money':15,
    'car_l':[],#如果用字典会好很多，方便统计数量
    'count':[],#保存对应的购物车里的商品数量
    'cupon':[] #优惠券
}
goods={1:{'name':'小瓶可乐','price':3.5},
       2:{'name':'大瓶可乐','price':5.5},
       3:{'name':'小瓶水','price':2.5},
       4:{'name':'大瓶水','price':3.5},
       5:{'name':'毛巾','price':10},
       6:{'name':'水杯','price':15},
       7:{'name':'笔记本','price':5},
       8:{'name':'老板椅','price':230},
       9:{'name':'牛里脊','price':50}
       }
#打印小票
def print_ticket():
    num = 0 #总金额
    fee = 0 #某种商品总价
    print("\t\t消费详情")
    print("商品名称\t\t价格\t\t数量\t\t费用")
    for i in range(len(user['car_l'])):
        if goods[user['car_l'][i]] == 0:
            continue
        name = goods[user['car_l'][i]]['name']
        count = user['count'][i]
        price = goods[user['car_l'][i]]['price']
        fee = count * price
        num += fee
        print(f'{name}\t\t{price}\t\t{count}\t\t{fee}')
    print(f'消费时间:{datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")}\n总计消费:{num}\t\t\t用户余额:{user['money']}')
# print_ticket()

#判断是否有商品存在
def isGoodsExist(id):
    if id in goods.keys():#商品存在返回True
        return True
    return False
# print(isGoodsExist(1))
"""
判断剩余钱是否足够购买商品
id:goods的key值
"""
def isMoneyEnough(id):
    price = goods[id]['price']
    money = user['money']
    if money >= price:
        return True
    return False

#添加上品到购物车,id是商品id
def addCar(id):
    if isGoodsExist(id):#判断商品是否存在于商场
        if isMoneyEnough(id):#判断钱是否足够
            if id in user['car_l']: #如果购物车里有同样的商品
                user['count'][user['car_l'].index(id)] +=1 #商品数量+1
            else:
                user['car_l'].append(id)
                user['count'].append(1)
            user['money'] = user['money'] - goods[id]['price']
            print(f'恭喜，购买成功！您的余额还剩{user['money']}。')
        else:
            print("穷鬼，钱不够！请到其他超市购买!")
    else:
        print("没有这个商品，别瞎弄！")
# addCar(1)
# addCar(1)
# print(user)

#打印商品货架
def printGoods():
    for i in goods.keys():
        print(f'商品id:{i}\t 商品名称:{goods[i]['name']}\t 商品价格:{goods[i]['price']}￥')
# printGoods()

#抽取优惠券随机从商品种抽一种打八折
def cupon():
    cupon1 = random.randint(1,len(goods)) #在货架上随即找一个数（商品id）
    if cupon1 not in user['cupon']:
        user['cupon'].append(cupon1) #将优惠券放入优惠券列表
        print(f'恭喜你获得了 {goods[cupon1]["name"]} 八折 优惠')
    else:
        while True:
            cupon2 = random.randint(1,len(goods))
            if cupon2 not in user['cupon']:
                print(f'恭喜你获得了 {goods[cupon2]["name"]} 八折 优惠')
                break
#根据id判断商品是否有对应的优惠券,如果有该商品费用打八折
# id 商品id
# fee 打折前的费用
def isCupon():
    for i in user['cupon']: #打折的商品id
        if i in goods.keys(): #传入的商品id在优惠券列表中
            goods[i]['price'] = round(0.8 * goods[i]['price'],2) #打八折

#退货
def backGoods(id):
    if id not in user['car_l']:
        print('商品不在购物车中')

    elif id in user['car_l'] and user['count'][user['car_l'].index(id)] == 1:#购物车中该商品只有一个
        del user['count'][user['car_l'].index(id)]  # 删除商品的数量
        del user['car_l'][user['car_l'].index(id)]  #从购物车移除该商品id
        user['money'] += goods[id]['price']

    else:
        user['count'][user['car_l'].index(id)] -= 1  # 该商品数量-1
        user['money'] += goods[id]['price']


